/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ryan.ohos.extension.nested;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentParent;

public class NestedScrollingHelper {
    /**
     * Indicates that the input type for the gesture is from a user touching the screen.
     */
    public static final int TYPE_TOUCH = 0;

    /**
     * Indicates that the input type for the gesture is caused by something which is not a user
     * touching a screen. This is usually from a fling which is settling.
     */
    public static final int TYPE_NON_TOUCH = 1;

    /**
     * Indicates no axis of view scrolling.
     */
    public static final int SCROLL_AXIS_NONE = 0;

    /**
     * Indicates scrolling along the horizontal axis.
     */
    public static final int SCROLL_AXIS_HORIZONTAL = 1 << 0;

    /**
     * Indicates scrolling along the vertical axis.
     */
    public static final int SCROLL_AXIS_VERTICAL = 1 << 1;

    public static void stopNestedScroll(Component view, int type) {
        if (view instanceof NestedScrollingChild) {
            ((NestedScrollingChild) view).stopNestedScroll(type);
        }
    }

    public static void onStopNestedScroll(ComponentParent parent, Component target, int type) {
        if (parent instanceof NestedScrollingParent) {
            ((NestedScrollingParent) parent).onStopNestedScroll(target, type);
        }
    }




    // --------parent---------

    public static boolean onStartNestedScroll(ComponentParent parent, Component child, Component target,
                                              int nestedScrollAxes, int type) {
        if (parent instanceof NestedScrollingParent) {
            return ((NestedScrollingParent) parent).onStartNestedScroll(child, target,
                    nestedScrollAxes, type);
        }
        return false;
    }

    public static void onNestedScrollAccepted(ComponentParent parent, Component child, Component target,
                                              int nestedScrollAxes, int type) {
        if (parent instanceof NestedScrollingParent) {
            ((NestedScrollingParent) parent).onNestedScrollAccepted(child, target,
                    nestedScrollAxes, type);
        }
    }

    public static void onNestedScroll(ComponentParent parent, Component target, int dxConsumed,
                                      int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type,
                                      int[] consumed) {
        if (parent instanceof NestedScrollingParent) {
            ((NestedScrollingParent) parent).onNestedScroll(target, dxConsumed, dyConsumed,
                    dxUnconsumed, dyUnconsumed, type, consumed);
        }
    }

    public static void onNestedPreScroll(ComponentParent parent, Component target, int dx, int dy,
                                         int[] consumed, int type) {
        if (parent instanceof NestedScrollingParent) {
            ((NestedScrollingParent) parent).onNestedPreScroll(target, dx, dy, consumed, type);
        }
    }

    public static boolean onNestedFling(ComponentParent parent, Component target, float velocityX,
                                        float velocityY, boolean consumed) {
        if (parent instanceof NestedScrollingParent) {
            return ((NestedScrollingParent) parent).onNestedFling(target, velocityX, velocityY,
                    consumed);
        }
        return false;
    }

    public static boolean onNestedPreFling(ComponentParent parent, Component target, float velocityX,
                                           float velocityY) {
        if (parent instanceof NestedScrollingParent) {
            return ((NestedScrollingParent) parent).onNestedPreFling(target, velocityX,
                    velocityY);
        }
        return false;
    }
}
